﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="IEnumerableBatchExtensionMethodTests.cs" company="DST Nexdox">
//   Copyright (c) DST Nexdox. All rights reserved.
// </copyright>
// <summary>
//   Defines the ExportFileServiceTests type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace ExtensionsTests
{
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using Extensions;
  using FluentAssertions;
  using NUnit.Framework;

  [TestFixture]
  public class IEnumerableBatchExtensionMethodTests
  {
    [SetUp]
    public void SetUp()
    {
    }

    [Test]
    public void GivenAnEmptySet_WhenBatching_NoBatchesAreReturned()
    {
      var emptyList = new List<Int32>();

      IEnumerable<IEnumerable<Int32>> results = emptyList.Batch(2);

      results.Count().Should().Be(0);
    }

    [Test]
    public void GivenASingleItem_WhenBatching_ASingleBatchOfOneItemIsReturned()
    {
      var oneItemList = new List<String> { "One" };

      IEnumerable<List<String>> result = oneItemList.Batch(2);

      List<List<String>> batches = result.ToList();
      batches.Count.Should().Be(1);

      List<String> items = batches[0];
      items.Count.Should().Be(1);
      items[0].Should().Be("One");
    }

    [Test]
    public void GivenFourItems_WhenBatchingInGroupsOfTwo_TwoBatchesAreReturned()
    {
      var fourItemList = new List<String> { "One", "Two", "Three", "Four" };

      IEnumerable<List<String>> result = fourItemList.Batch(2);

      List<List<String>> batches = result.ToList();
      batches.Count.Should().Be(2);

      batches[0].Count.Should().Be(2);
      batches[0][0].Should().Be("One");
      batches[0][1].Should().Be("Two");

      batches[1].Count.Should().Be(2);
      batches[1][0].Should().Be("Three");
      batches[1][1].Should().Be("Four");
    }

    [Test]
    public void GivenSevenItems_WhenBatchingInGroupsOfThree_TwoFullBatchesAndOnePartialBatchIsReturned()
    {
      var sevenItemList = new List<String> { "One", "Two", "Three", "Four", "Five", "Six", "Seven" };

      IEnumerable<List<String>> result = sevenItemList.Batch(3);

      List<List<String>> batches = result.ToList();
      batches.Count.Should().Be(3);

      batches[0].Count.Should().Be(3);
      batches[0][0].Should().Be("One");
      batches[0][1].Should().Be("Two");
      batches[0][2].Should().Be("Three");

      batches[1].Count.Should().Be(3);
      batches[1][0].Should().Be("Four");
      batches[1][1].Should().Be("Five");
      batches[1][2].Should().Be("Six");

      batches[2].Count.Should().Be(1);
      batches[2][0].Should().Be("Seven");
    }
  }
}
